package com.timeDeal.servlet;





import com.alibaba.druid.util.StringUtils;
import com.timeDeal.bean.PinglunBean;
import com.timeDeal.bean.ShijianchushouBean;
import com.timeDeal.bean.UserBean;
import com.timeDeal.bean.XuqiufabuBean;
import com.timeDeal.service.*;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/7/24.
 */

public class DetailServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException, IOException {
        PinglunServicce ps = new PinglunServicce() ;
        Date  date = new Date(new java.util.Date().getTime()) ;
        PinglunBean pb = new PinglunBean();
        UserService us = new UserService() ;
        XuqiuService xuqiuService = new XuqiuService() ;
        List<PinglunBean>  pinglun = new ArrayList<>() ;
        List<String>  name = new ArrayList<>() ;
        UserBean ub = (UserBean)request.getSession().getAttribute("USER");
        int id = Integer.valueOf( request.getParameter("id"));
        String  pinglunneirong =request.getParameter("wodepinglun");
        String leixing = request.getParameter("leixing");
        System.out.println(leixing+"OOOOOOOOOOOOOOOOOOOOOOO");
        int currentpage = Integer.valueOf( request.getParameter("currentpage"));
        pb.setPinglunriqi(date);
        pb.setPinglunneirong(pinglunneirong);
        pb.setPinglunrenid(ub.getYonghuid());
        if(StringUtils.equals("xuqiufabu",leixing)){
            pb.setXuqiufabuid(id);
        }
        else { pb.setShijianchushouid(id);}
        if (us.addAPinglun(pb, leixing)) {

               if(StringUtils.equals("xuqiufabu",leixing)) {
                   xuqiuService.zengJiaRedu(pb.getXuqiufabuid()) ;
                   pinglun = ps.getAllPinglunByLeixingId(Integer.valueOf(id), leixing ,currentpage);
                   for (int i = 0; i < pinglun.size(); i++) {
                       name.add(us.getDetailById(pinglun.get(i).getPinglunrenid()));}
                    XuqiuService xs = new XuqiuService() ;
                    XuqiufabuBean xfb = xs.getAxuquById(Integer.valueOf(id));
                    request.setAttribute("xuqiu",xfb);
                    request.setAttribute("tijiaoren",xfb.getTijiaorenXingming());
                }
                else {
                    ShijianchusouService shijianchusouService = new ShijianchusouService() ;
                   shijianchusouService.zengJiaRedu(pb.getShijianchushouid()) ;
                   pinglun = ps.getAllPinglunById(Integer.valueOf(id) ,currentpage);
                   for (int i = 0; i < pinglun.size(); i++) {
                       name.add(us.getDetailById(pinglun.get(i).getPinglunrenid()));}
                   UserService userService = new UserService() ;
                   ShijianchushouBean shijianchushouBean = shijianchusouService.getAShijianChushouById(Integer.valueOf(id));
                   shijianchushouBean.setTijiaorenXingming(userService.getAuserByid(Integer.valueOf(id)).getYonghuming());
                   request.setAttribute("shijian",shijianchushouBean);
                   request.setAttribute("tijiaoren",shijianchushouBean.getTijiaorenXingming());
               }
                System.out.println(ps.getCountByLeixingId(leixing,Integer.valueOf(id)));
                System.out.println();
                System.out.println(ps.getCountByLeixingId(leixing,Integer.valueOf(id)));
                System.out.println(ps.getCountByLeixingId(leixing,Integer.valueOf(id)));
                request.setAttribute("MAX", ps.getCountByLeixingId(leixing,Integer.valueOf(id)));
                request.setAttribute("name", name);
                request.setAttribute("id", String.valueOf(id));
                request.setAttribute("pinglun", pinglun);
                request.setAttribute("currentpage",String.valueOf(currentpage));
                PrintWriter out = response.getWriter();
                out.print("<script>alert(pinglun  sucessful!);</script>");
                if(StringUtils.equals(leixing,"shijianchushou")){
                request.getRequestDispatcher("/user/shijianchushou/pinglun.jsp").forward(request,response);}
                else {request.getRequestDispatcher("/user/pinglun.jsp").forward(request,response);}
            } else {
                PrintWriter out = response.getWriter();
                out.print("<script contentType=“text/html;charset=UTF-8”>alert(\"pinglun error!\");</script>");
                response.setHeader("refresh", "0;url=/TimeDeal/user/pinglun.jsp");
            }

    }
}